using System.Collections.Generic;
using System.Threading.Tasks;
using SQLite;
using t01.Models;

namespace t01.Services;

public class PoetryStorage : IPoetryStorage
{
    // the db instance, and compile time
    public const string DBINST = "poetrydb.sqlite3";

    public static readonly string PoetryDBPath = "poetrydb.sqlite3";

    private SQLiteAsyncConnection? _connection;

    private SQLiteAsyncConnection Connection =>
        _connection ??= new SQLiteAsyncConnection(PoetryDBPath);

    // create table
    public async Task InitializeAsync()
    {
        await Connection.CreateTableAsync<Poetry>();
    }




    // insert data
    public async Task InsertAsync(Poetry poetry) =>
        await Connection.InsertAsync(poetry);

    public async Task<IList<Poetry>> ListAsync() => await Connection.Table<Poetry>().ToListAsync();

    // select * from Poetry where name like '%keyword%'
    public async Task<IList<Poetry>> QueryAsync(string keyword) => await Connection.Table<Poetry>().Where(p => p.Name.Contains(keyword)).ToListAsync();
}